package com.wn26.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wn26.bean.User;
import com.wn26.service.UserService;
import com.wn26.service.impl.UserServiceImpl;
@WebServlet(name = "UserRegisterServlet", urlPatterns = { "/userRegister" })
public class UserRegisterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 设置编码格式
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");

		// --1.利用请求对象接受参数

		String user_name = req.getParameter("user_name");
		String user_pwd = req.getParameter("user_pwd");
		String user_tel = req.getParameter("user_tel");
		System.out.println(user_name + user_pwd + user_tel);
		User user = new User(0, user_name, user_pwd, user_tel);
		// --2.调用业务类中的方法
		UserService us = new UserServiceImpl();
		try {
			boolean update = us.userRegister(user);			
			if (update) {
				System.out.println("注册成功");
				req.getRequestDispatcher("index.html").forward(req, resp);
			} else {
				System.out.println("注册失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}